package cn.com.zcode.core.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class ObjectUtil {
	public static void storeObject(String path, Object obj) {
		ObjectOutputStream out = null;
		FileOutputStream fout = null;
		try {
			fout = new FileOutputStream(path);
			out = new ObjectOutputStream(fout);
			out.writeObject(obj);
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			close(fout);
			close(out);
		}

	}

	@SuppressWarnings("unchecked")
	public static <T> T readObject(String path, Class<? extends T> zlass) {

		ObjectInputStream oin = null;
		FileInputStream fin = null;
		Object obj = null;
		try {
			fin = new FileInputStream(path);
			oin = new ObjectInputStream(fin);
			obj = oin.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(fin);
			close(oin);
		}

		return (T) obj;

	}

	private static void close(OutputStream out) {
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static void close(InputStream out) {
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(RandomAccessFile file) {
		try {
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void saveFile(File file, String str) {
		BufferedWriter buw = null;

		try {
			FileWriter writer = new FileWriter(file);
			buw = new BufferedWriter(writer);
			buw.write(str);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			close(buw);
		}
	}

	private static void close(Writer buw) {

		if (buw != null) {
			try {
				buw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static String readFile(File file) {
		FileReader bin = null;
		StringBuffer buffer = new StringBuffer();
		try {
			bin = new FileReader(file);
			BufferedReader reader = new BufferedReader(bin);
			String str = null;
			while ((str = reader.readLine()) != null) {
				buffer.append(str);
				buffer.append("\n");
			}
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			close(bin);

		}
		return buffer.toString();

	}

	private static void close(Reader bin) {
		if (bin != null) {
			try {
				bin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
